import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../../assets/styles.dart';
import '../../../../generate/app_image_path.dart';
import '../../../../services/user_service.dart';
import '../../../../utils/color.dart';
import '../../../../utils/consts.dart';
import '../../../../utils/extension.dart';
import '../../common/ai_cost_tips.dart';
import '../../common/ai_picker_icon.dart';
import '../../common/ai_utils.dart';
import '../../controllers/tabs/ai_tab_face_custom_page_controller.dart';

class AiTabFaceCustomPage extends GetView<AiTabFaceCustomPageController> {
  const AiTabFaceCustomPage({super.key});

  Widget _buildPickTargetTips() {
    return Text(
      '上传您需要换脸的图片',
      style: TextStyle(
        fontWeight: FontWeight.w500,
        fontSize: 14.w,
      ),
    );
  }

  Widget _buildPickTarget() {
    return Container(
      height: 180.w,
      decoration: BoxDecoration(
        borderRadius: Styles.borderRadius.all(8.w),
        color: COLOR.white.withValues(alpha: 0.1),
      ),
      clipBehavior: Clip.hardEdge,
      child: Obx(
        () {
          final image = controller.targetImage.value;
          if (image.isEmpty) {
            return Center(
                child: AiPickerIcon(onTap: controller.onTapTargetPicker));
          } else {
            return Stack(
              children: [
                const SizedBox.expand(),
                Positioned.fill(
                  child: Center(
                    child: Image.memory(
                      image.bytes,
                      fit: BoxFit.cover,
                      width: 112.w,
                    ),
                  ),
                ),
                Positioned(
                  right: 6.w,
                  top: 6.w,
                  child: Image.asset(
                    AppImagePath.ai_file_pick_close,
                    width: 22.w,
                    height: 22.w,
                  ).onTap(controller.onTapTargetClear),
                )
              ],
            );
          }
        },
      ),
    );
  }

  Widget _buildZhuYiShiXiang() {
    final titleStyle = TextStyle(fontSize: 14.w, fontWeight: FontWeight.w500);
    final tipStyle = TextStyle(fontSize: 14.w, color: COLOR.color_9B9B9B);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('注意事项', style: titleStyle),
        Text('1.照片只含一名人物', style: tipStyle),
        Text('2.图片不能过暗', style: tipStyle),
        Text('3.图片尽量清晰，否则会出现失败或者影响效果', style: tipStyle),
      ].joinHeight(7.w),
    );
  }

  Widget _buildPickStencilTips() => Text(
        '上传脸部图片',
        style: TextStyle(fontSize: 14.w, fontWeight: FontWeight.w500),
      );

  Widget _buildPickStencil() => Container(
        height: 180.w,
        decoration: BoxDecoration(
          borderRadius: Styles.borderRadius.all(8.w),
          color: COLOR.white.withValues(alpha: 0.1),
        ),
        clipBehavior: Clip.hardEdge,
        child: Obx(
          () {
            final image = controller.stencilImage.value;
            if (image.isEmpty) {
              return Center(
                child:
                    AiPickerIcon.example(onTap: controller.onTapStencilPicker),
              );
            } else {
              return Stack(
                children: [
                  const SizedBox.expand(),
                  Positioned.fill(
                    child: Center(
                      child: Image.memory(
                        image.bytes,
                        fit: BoxFit.cover,
                        width: 112.w,
                      ),
                    ),
                  ),
                  Positioned(
                    right: 6.w,
                    top: 6.w,
                    child: Image.asset(
                      AppImagePath.ai_file_pick_close,
                      width: 22.w,
                      height: 22.w,
                    ).onTap(controller.onTapStencilClear),
                  )
                ],
              );
            }
          },
        ),
      );

  Widget _buildMakeRow() => Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Obx(
            () => AiCostTips(
              gold: Consts.aiFaceCustomCostGold,
              freeCount: Get.find<UserService>().user.aiNum ?? 0,
            ),
          ),
          AiUtils.buildMakeButton(onTap: controller.onTapMake)
        ],
      );

  Widget _buildBody() => CustomScrollView(
        slivers: [
          30.verticalSpaceFromWidth.sliver,
          _buildPickTargetTips().sliver,
          12.verticalSpaceFromWidth.sliver,
          _buildPickTarget().sliver,
          // 11.verticalSpaceFromWidth.sliver,
          // const AiImageLimitTips().sliver,
          20.verticalSpaceFromWidth.sliver,
          _buildZhuYiShiXiang().sliver,
          20.verticalSpaceFromWidth.sliver,
          _buildPickStencilTips().sliver,
          6.verticalSpaceFromWidth.sliver,
          _buildPickStencil().sliver,
          20.verticalSpaceFromWidth.sliver,
          _buildMakeRow().sliver,
          40.verticalSpaceFromWidth.sliver
        ],
      );

  @override
  Widget build(BuildContext context) => _buildBody();
}
